package com.example.cyw.mycontextprover;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by cyw on 2017/12/4.
 */

public class MyContentProvider extends ContentProvider {
    public  static  final  String URI_STUDENT="content://com.gdcp.studb/student";
    static  private  final String  AUTHORITY = "com.gdcp.studb";
    static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
    static {
        matcher.addURI( AUTHORITY ,"student",1);
    }
    private  myOpenHelper helper;
    @Override
    public boolean onCreate() {
        helper=new myOpenHelper(getContext(),"stu.db",null,1);
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        switch (matcher.match(uri)){
            case 1:
                SQLiteDatabase db=helper.getReadableDatabase();
                Cursor cur=db.query("student",projection,selection,selectionArgs,null,null,sortOrder);
                return  cur;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        switch (matcher.match(uri)){
            case 1:
                SQLiteDatabase db=helper.getWritableDatabase();
                db.insert("student",null,values);
                db.close();
                break;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        switch (matcher.match(uri)){
            case 1:
                SQLiteDatabase db=helper.getWritableDatabase();
                db.delete("student",selection,selectionArgs);
                db.close();
                break;
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
